# what we want here is to preview information about a specific book
def book():
    ''' information like the books details, the current requests and the status of the book is pretty important here'''
    try:
        bookID = int(request.args[0])
        if len(request.args) > 1 or bookID <= 0:
            raise Exception("Invalid Arguements were passed")
    except:
        redirect(URL(r=request, c='default', f='index', vars=dict(error=T("Invalid Request To Specified Page"))))
    
    book = db((db.books.id==bookID) & (db.books.owner==db.users.id)).select(db.books.ALL, db.users.id, db.users.username, db.users.first_name, db.users.last_name)

    if len(book):
        requests = db((db.readRequests.bookID==book[0].books.id) & (db.users.id==db.readRequests.requestBy)).select(db.readRequests.ALL, db.users.id, db.users.username, db.users.first_name, db.users.last_name, orderby=db.readRequests.requestedOn)
        return dict(book=book, requests=requests)        
    else:
    
        return dict(book=book)

@auth.requires_login()
def cancel():
    try:
        bookID = int(request.args[0])
        if len(request.args) > 1 or bookID <= 0:
            raise Exception("Invalid Arguements were passed")
    except:
        redirect(URL(r=request, c='default', f='index', vars=dict(error=T("Invalid Request To Specified Page"))))
        
    book = db((db.books.id==bookID) & (db.books.owner==db.users.id)).select(db.books.ALL, db.users.id, db.users.username, db.users.first_name, db.users.last_name)
    
    if len(book):
        myBook = book[0]
        rows = db((db.readRequests.bookID == myBook.books.id) & (db.readRequests.requestBy==auth.user.id)).select(db.readRequests.ALL)
        if not len(rows):
            session.flash = "Already NOT Requested"
            redirect(URL(r=request, c='books', f='book', args=[book[0].books.id]))
            
        db(db.readRequests.id == rows[0].id).delete()
        db.log.insert(byUser=auth.user.id, onWhat='BOOK', onWho=book[0].books.id, operation='CANCEL')
        session.flash = "Cancelled"
        redirect(URL(r=request, c='books', f='book', args=[book[0].books.id]))
    else:
        redirect(URL(r=request, c='books', f='book', vars=dict(error=T("Invalid Request To Specified Page"))))
    
        
@auth.requires_login()       
def borrow():
    try:
        bookID = int(request.args[0])
        if len(request.args) > 1 or bookID <= 0:
            raise Exception("Invalid Arguements were passed")
    except:
        redirect(URL(r=request, c='default', f='index', vars=dict(error=T("Invalid Request To Specified Page"))))
        
    book = db((db.books.id==bookID) & (db.books.owner==db.users.id)).select(db.books.ALL, db.users.id, db.users.username, db.users.first_name, db.users.last_name)
    
    if len(book):
        myBook = book[0]
        rows = db((db.readRequests.bookID == myBook.books.id) & (db.readRequests.requestBy==auth.user.id)).select(db.readRequests.ALL)
        if len(rows):
            session.flash = "Already Requested"
            redirect(URL(r=request, c='books', f='book', args=[book[0].books.id]))
        db.readRequests.insert(bookID=book[0].books.id, requestBy=auth.user.id)
        db.log.insert(byUser=auth.user.id, onWhat='BOOK', onWho=book[0].books.id, operation='REQUEST')
        session.flash = "Requested"
        redirect(URL(r=request, c='books', f='book', args=[book[0].books.id]))
    else:
        redirect(URL(r=request, c='books', f='book', vars=dict(error=T("Invalid Request To Specified Page"))))
        
def photo():
    import os
    record = db(db.books.id==request.args[0]).select(db.books.photo)
    if not len(record) or not record[0].photo:
        filename = os.path.join(request.folder, "static", "nophoto.png")
        return response.stream(filename)
    filename = os.path.join(request.folder, "uploads", record[0].photo)
    return response.stream(filename)
